package com.yunhe.action;

import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.yunhe.utils.DateUtil;
import com.yuunhe.entity.User;
import com.yuunhe.service.UserBiz;
import com.yuunhe.serviceimpl.UserBizImpl;

public class UserAction extends ActionSupport {
	private UserBiz ub = new UserBizImpl();
	private User user = new User();
	private int id;
	private String name;
	private String address;
	private String birth;
	private int age;
	
	public void setId(int id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public void setBirth(String birth) {
		this.birth = birth;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	public String getAddress() {
		return address;
	}
	public String getBirth() {
		return birth;
	}
	public int getAge() {
		return age;
	}

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	public String findAll() {
		try {
			List<User> list = ub.getAll();
			ServletActionContext.getRequest().setAttribute("userlist", list);
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException("输出异常");
		}
	}

	public void deleteUser() {
		try {
			id=Integer.parseInt(ServletActionContext.getRequest().getParameter("id"));
			ub.deleteUser(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException("删除异常！！！");
		}
	}

	public String addUser() {
		try {
			user.setName(name);
			user.setAddress(address);
			user.setAge(age);
			user.setBirth(DateUtil.string2Date(birth));
			ub.addUser(user);
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("添加失败！！！");
		}

	}
}
